package com.example.fuluchou.controller;

import com.example.fuluchou.common.errorcode.ControllerErrorCodeConstants;
import com.example.fuluchou.common.exception.ControllerException;
import com.example.fuluchou.common.pojo.CommonResult;
import com.example.fuluchou.common.untils.JacksonUtil;
import com.example.fuluchou.controller.param.CreatePrizeParam;
import com.example.fuluchou.controller.param.PageParam;
import com.example.fuluchou.controller.result.FindPrizeListResult;
import com.example.fuluchou.service.PictureService;
import com.example.fuluchou.service.PrizeService;

import com.example.fuluchou.service.dto.PageListDTO;
import com.example.fuluchou.service.dto.PrizeDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;


@RestController
public class PrizeController {
    private static final Logger logger = LoggerFactory.getLogger(PrizeController.class);

    @Autowired
    PictureService pictureService;

    @Autowired
    PrizeService prizeService;

    @RequestMapping("/pic/upload")
    public String uploadPic(@RequestParam(value = "file") MultipartFile file){
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     * @RequestPart("param"):用于接受表单数据 multipart/from-data
     *
     * @param param
     * @param prizePic
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(
            @Validated  @RequestPart("param")CreatePrizeParam param,
            @RequestPart("prizePic") MultipartFile prizePic){
        logger.info("create prize: {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(prizeService.createPrize(param,prizePic));
    }

    //TD:需要实现全量获取功能，获取全部奖品信息功能，用于create-activity页面奖品模版框

    //此接口是实现翻页奖品信息
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param){
        logger.info("findPrizeList FindPrizeListParam:{}",JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));


    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if(null == pageListDTO){
            throw new ControllerException(ControllerErrorCodeConstants.PAGE_FIND_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(pageListDTO.getRecords().stream()
                .map(prizeDTO->{
                    FindPrizeListResult.PrizeInfo prizeInfo =new FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    prizeInfo.setImageUrl(prizeDTO.getImage_url());
                    return prizeInfo;
                }).collect(Collectors.toList())
        );

        return result;
    }
}
